[20220812_155354_user] D2_main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. * @attention
  7. *
  8. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  9. * All rights reserved.</center></h2>
  10. *
  11. * This software component is licensed by ST under BSD 3-Clause license,
  12. * the License; You may not use this file except in compliance with the
  13. * License. You may obtain a copy of the License at:
  14. * www.st.com/SLA0044
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "adc.h"
  22. #include "usart.h"
  23. #include "i2c.h"
  24. #include "tim.h"
  25. #include "stm32f4xx_hal.h"
  26. #include "gpio.h"
  27. #include "stdio.h"
  28. /* Private variables ---------------------------------------------------------*/
  29. /* Private variables ---------------------------------------------------------*/
  30. #define VECT_TAB_OFFSET 0x10000
  31. int tankstatus = 0;
  32. uint8_t zigbeecmd1[5]={0xFE,0x00,0x01,0x00,0x01};
  33. uint8_t zigbeecmd2[5]={0xFE,0x00,0x02,0x00,0x02};
  34. uint8_t zigbeestatu[20];
  35. uint8_t zigbeestatu1[5];
  36. uint8_t zigbeecon[7]="connect";
  37. uint8_t zigbeenotcon[11]="not connect";
  38. uint8_t zigbeechang[14]="change is down";
  39. uint8_t cmd[7]={0xFF,0x00,0x00,0x00,0x00,0x00,0x0D};
  40. uint16_t i,val,value_H,value_L,Cvalue,Hvalue;
  41. int error;
  42. float C1=-2.0468;
  43. float C2=0.0367;
  44. float C3=-0.0000015955;
  45. float RH_Lin;
  46. float RH_Ture;
  47. float d1=-39.6;
  48. float d2=0.01;
  49. float T1=0.01;
  50. float T2=0.00008;
  51. float temp_C=0;
  52. int temp;
  53. int RH;
  54. float M14_SHT11 =0;
  55. /* USER CODE BEGIN PV */
  56. typedef void (*pFunction)(void);
  57. /* USER CODE END PV */
  58. /* Private function prototypes -----------------------------------------------*/
  59. void SystemClock_Config(void);
  60. void MX_ADC1_Init1(char pin);
  61. void sensor(void);
  62. void SHT10_TransStart(void);
  63. void SHT10_WriteByte(void);
  64. void SHT10_WriteByte2(void);
  65. void SHT10_ReadByte(void);
  66. void SHT10_Calculate(void);
  67. void MX_GPIO_Input1(unsigned long pin);
  68. /* USER CODE BEGIN PFP */
  69. /* USER CODE END PFP */
  70. /* Private user code ---------------------------------------------------------*/
  71. /* USER CODE BEGIN 0 */
  72. int _write(int fd, char *ch, int len)
  73. {
  74. HAL_UART_Transmit(&huart4, (uint8_t*)ch, len, 0xFFFF);
  75. HAL_UART_Transmit(&huart2, (uint8_t*)ch, len, 0xFFFF);
  76. return len;
  77. }
  78. void print(char* s, float c)
  79. { int w;
  80. int w_d;
  81. int w_f;
  82. if(c<0)
  83. {
  84. w = c*100;
  85. w_d = w/100;
  86. w_f = (-1*w)%100;
  87. }
  88. else
  89. {
  90. w = c*100;
  91. w_d = w/100;
  92. w_f = w%100;
  93. }
  94. printf("%s = %d.%d\n",s,w_d,w_f);
  95. }
  96. /* USER CODE END 0 */
  97. /**
  98. * @brief The application entry point.
  99. * @retval int
  100. */
  101. int main(void)
  102. {
  103. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET;
  104. HAL_Init();
  105. SystemClock_Config();
  106. MX_GPIO_Init();
  107. MX_USART2_UART_Init();
  108. MX_UART4_Init();
  109. MX_USART1_UART_Init();
  110. HAL_UART_Transmit(&huart2, (uint8_t *)zigbeecmd2, sizeof(zigbeecmd2), 10);
  111. HAL_Delay(1000);
  112. HAL_UART_Transmit(&huart2, (uint8_t *)zigbeecmd1, sizeof(zigbeecmd1), 10);
  113. HAL_UART_Receive(&huart2, (uint8_t *)zigbeestatu,sizeof(zigbeestatu),2000);
  114. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeestatu, sizeof(zigbeestatu),10);
  115. if (zigbeestatu[18]== 0x01 || zigbeestatu[18]== 0x02)
  116. {
  117. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeecon, sizeof(zigbeecon),10);
  118. }
  119. else if(zigbeestatu[18]== 0x05)
  120. {
  121. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeenotcon, sizeof(zigbeenotcon),10);
  122. }
  123. while (1)
  124. {
  125. sensor();
  126. if(M14_SHT11<16)
  127. {
  128. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_10, GPIO_PIN_RESET);
  129. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8, GPIO_PIN_RESET);
  130. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15, GPIO_PIN_RESET);
  131. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_13, GPIO_PIN_RESET);
  132. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_11, GPIO_PIN_RESET);
  133. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15, GPIO_PIN_RESET);
  134. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15, GPIO_PIN_RESET);
  135. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13, GPIO_PIN_RESET);
  136. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_11, GPIO_PIN_RESET);
  137. HAL_Delay(5000);
  138. }
  139. }
  140. }
  141. void SystemClock_Config(void)
  142. {
  143. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  144. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  145. __HAL_RCC_PWR_CLK_ENABLE();
  146. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  147. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  148. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  149. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  150. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  151. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  152. RCC_OscInitStruct.PLL.PLLM = 8;
  153. RCC_OscInitStruct.PLL.PLLN = 72;
  154. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  155. RCC_OscInitStruct.PLL.PLLQ = 3;
  156. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  157. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  158. {
  159. Error_Handler();
  160. }
  161. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  162. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  163. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  164. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  165. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  166. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  167. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  168. {
  169. Error_Handler();
  170. }
  171. }
  172. void user_pwm_setvalue(uint16_t value)
  173. {
  174. TIM_OC_InitTypeDef sConfigOC;
  175. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  176. sConfigOC.Pulse = value;
  177. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  178. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  179. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);
  180. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
  181. }
  182. void user_pwm_setvalue2(uint16_t value)
  183. {
  184. TIM_OC_InitTypeDef sConfigOC;
  185. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  186. sConfigOC.Pulse = value;
  187. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  188. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  189. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
  190. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
  191. }
  192. void sensor(void)
  193. {
  194. Cvalue=0;
  195. Hvalue=0;
  196. value_H=0;
  197. value_L=0;
  198. SHT10_TransStart();
  199. SHT10_WriteByte();
  200. MX_GPIO_Input1(GPIO_PIN_0);
  201. HAL_Delay(250);
  202. if (HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==0)
  203. {
  204. SHT10_ReadByte();
  205. value_H=val;
  206. SHT10_ReadByte();
  207. value_L=val;
  208. Cvalue = (value_H<< 8 | value_L);
  209. }
  210. SHT10_TransStart();
  211. SHT10_WriteByte2();
  212. MX_GPIO_Input1(GPIO_PIN_0);
  213. HAL_Delay(250);
  214. if (HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==0)
  215. {
  216. SHT10_ReadByte();
  217. value_H=val;
  218. SHT10_ReadByte();
  219. value_L=val;
  220. Hvalue = (value_H<< 8 | value_L);
  221. }
  222. SHT10_Calculate();
  223. HAL_Delay(10000);
  224. }
  225. void SHT10_TransStart(void)
  226. {
  227. MX_GPIO_Init1();
  228. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
  229. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  230. HAL_Delay(10);
  231. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  232. HAL_Delay(10);
  233. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
  234. HAL_Delay(10);
  235. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  236. HAL_Delay(10);
  237. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  238. HAL_Delay(10);
  239. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
  240. HAL_Delay(10);
  241. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  242. HAL_Delay(10);
  243. }
  244. void SHT10_WriteByte(void)
  245. {
  246. MX_GPIO_Init1();
  247. for (i=0x80;i>0;i/=2)
  248. {
  249. if (i & 0x03)
  250. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
  251. else
  252. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
  253. HAL_Delay(10);
  254. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  255. HAL_Delay(10);
  256. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  257. HAL_Delay(10);
  258. }
  259. MX_GPIO_Input1(GPIO_PIN_0);
  260. HAL_Delay(10);
  261. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  262. HAL_Delay(10);
  263. error=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);
  264. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  265. HAL_Delay(10);
  266. }
  267. void SHT10_WriteByte2(void)
  268. {
  269. MX_GPIO_Init1();
  270. for (i=0x80;i>0;i/=2)
  271. {
  272. if (i & 0x05)
  273. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
  274. else
  275. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
  276. HAL_Delay(10);
  277. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  278. HAL_Delay(10);
  279. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  280. HAL_Delay(10);
  281. }
  282. MX_GPIO_Input1(GPIO_PIN_0);
  283. HAL_Delay(10);
  284. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  285. HAL_Delay(10);
  286. error=HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0);
  287. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  288. HAL_Delay(10);
  289. }
  290. void SHT10_ReadByte(void)
  291. {
  292. val=0;
  293. MX_GPIO_Input1(GPIO_PIN_0);
  294. for (i=0x80;i>0;i/=2)
  295. {
  296. HAL_Delay(10);
  297. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  298. HAL_Delay(10);
  299. if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0))
  300. val=( val | i );
  301. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  302. }
  303. MX_GPIO_Init1();
  304. if (1)
  305. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
  306. else
  307. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
  308. HAL_Delay(10);
  309. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  310. HAL_Delay(10);
  311. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  312. HAL_Delay(10);
  313. }
  314. void SHT10_Calculate(void)
  315. {
  316. temp_C=d1+d2*Cvalue;
  317. RH_Lin = C1+C2*Hvalue+C3*Hvalue*Hvalue;
  318. RH_Ture= (temp_C-25)*(T1+T2*Hvalue)+RH_Lin;
  319. if (RH_Ture>100)
  320. RH_Ture= 100;
  321. if (RH_Ture<0.1)
  322. RH_Ture=0.1;
  323. if (temp_C<0)
  324. {
  325. cmd[1]=1;
  326. cmd[2]=-1*temp_C;
  327. temp=(-10*temp_C);
  328. cmd[3]=temp%10;
  329. cmd[4]=RH_Ture;
  330. RH=RH_Ture*10;
  331. cmd[5]=RH%10;
  332. }
  333. else
  334. {
  335. cmd[1]=0;
  336. cmd[2]=temp_C;
  337. temp=(10*temp_C);
  338. cmd[3]=temp%10;
  339. cmd[4]=RH_Ture;
  340. RH=RH_Ture*10;
  341. cmd[5]=RH%10;
  342. }
  343. }
  344. void MX_ADC1_Init1(char pin)
  345. {
  346. ADC_ChannelConfTypeDef sConfig = {0};
  347. hadc1.Instance = ADC1;
  348. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
  349. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  350. hadc1.Init.ScanConvMode = DISABLE;
  351. hadc1.Init.ContinuousConvMode = DISABLE;
  352. hadc1.Init.DiscontinuousConvMode = DISABLE;
  353. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  354. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  355. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  356. hadc1.Init.NbrOfConversion = 1;
  357. hadc1.Init.DMAContinuousRequests = DISABLE;
  358. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  359. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  360. {
  361. Error_Handler();
  362. }
  363. sConfig.Channel = pin;
  364. sConfig.Rank = 1;
  365. sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  366. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  367. {
  368. Error_Handler();
  369. }
  370. }
  371. void MX_GPIO_Input1(unsigned long pin)
  372. {
  373. GPIO_InitTypeDef GPIO_InitStruct = {0};
  374. __HAL_RCC_GPIOE_CLK_ENABLE();
  375. GPIO_InitStruct.Pin = pin;
  376. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  377. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  378. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  379. }
  380. void Error_Handler(void)
  381. {
  382. }